package org.manon.json.util;


/**
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 */
public final class NumberUtil {

	
	private NumberUtil() {}

	public static int parseInt(String value) {
		int unit = 1;
		int i = value.length()-1;
		int v = (value.charAt(i) ^ 0x30);
		i--;
		
		if (value.charAt(0) == '-') {
			for (; i > 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
			v *= -1;
		} else {
			for (; i >= 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
		}
		return v;
	}
	
	public static long parseLong(String value) {
		long unit = 1;
		int i = value.length()-1;
		long v = (value.charAt(i) ^ 0x30);
		i--;
		
		if (value.charAt(0) == '-') {
			for (; i > 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
			v *= -1;
		} else {
			for (; i >= 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
		}
		return v;
	}
	
	public static Integer parseInteger(String value) {
		int unit = 1;
		int i = value.length()-1;
		int v = (value.charAt(i) ^ 0x30);
		i--;
		
		if (value.charAt(0) == '-') {
			for (; i > 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
			v *= -1;
		} else {
			for (; i >= 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
		}
		return Integer.valueOf(v);
	}
	
	public static Long parseLongOb(String value) {
		long unit = 1;
		int i = value.length()-1;
		long v = (value.charAt(i) ^ 0x30);
		i--;
		
		if (value.charAt(0) == '-') {
			for (; i > 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
			v *= -1;
		} else {
			for (; i >= 0; i--) {
				v += ((value.charAt(i) ^ 0x30) * (unit *= 10));
			}
		}
		return Long.valueOf(v);
	}
	
}